//
//  ByNormalPushAnimator.m
//  Private_Object
//
//  Created by pro on 16/9/10.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByNormalPushAnimator.h"

@implementation ByNormalPushAnimator

- (void)xw_setToAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIView *containerView = [transitionContext containerView];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    CGRect initialFrame = [transitionContext initialFrameForViewController:fromVC];
    CGRect finialFrame = [transitionContext finalFrameForViewController:toVC];
    
    UIView *mask = [[UIView alloc] init]; // 黑色半透明视图
    mask.backgroundColor = [UIColor blackColor];
    [containerView addSubview:mask];
    [containerView addSubview:toVC.view];
    
    
    [containerView sendSubviewToBack:fromVC.view];
    [containerView bringSubviewToFront:toVC.view];
    
    fromVC.view.frame = initialFrame;
    mask.frame = fromVC.view.frame;
    mask.alpha = 0.0;
    toVC.view.frame = finialFrame;
    toVC.view.transform = CGAffineTransformMakeTranslation(SCREEN_WIDTH, 0);
    
    [UIView animateWithDuration:self.toDuration
                     animations:^{
                         
                         
                         mask.alpha = 0.4;
                         toVC.view.transform = CGAffineTransformIdentity;
                         toVC.view.layer.transform = CATransform3DIdentity;
                         fromVC.view.frame = CGRectOffset(initialFrame, -SCREEN_WIDTH / 4, 0);
                         mask.frame = fromVC.view.frame;
                         
                     }
                     completion:^(BOOL finished) {
                         
                         [mask removeFromSuperview];
                         
                         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                         
                     }];
}



- (void)xw_setBackAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIView *containerView = [transitionContext containerView];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    CGRect finialFrame = [transitionContext finalFrameForViewController:toVC];
    
    UIView *mask = [[UIView alloc] init]; // 黑色半透明视图
    mask.backgroundColor = [UIColor blackColor];
    [containerView addSubview:mask];
    [containerView addSubview:toVC.view];
    
    [containerView sendSubviewToBack:toVC.view];
    [containerView bringSubviewToFront:fromVC.view];
    
    mask.alpha = 0.4;
    toVC.view.frame = CGRectOffset(finialFrame, -SCREEN_WIDTH / 4, 0);;
    mask.frame = toVC.view.frame;
  
    [UIView animateWithDuration:self.backDuration
                     animations:^{
                         
                         mask.alpha = 0.0;
                         toVC.view.frame = finialFrame;
                         mask.frame = toVC.view.frame;
                         fromVC.view.transform = CGAffineTransformMakeTranslation(SCREEN_WIDTH, 0);
                     }
                     completion:^(BOOL finished) {
                         
                         [mask removeFromSuperview];
                         
                         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                         
                     }];
    
}


@end
